package fr.km.client.ui;

import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.HasCloseHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

import fr.km.client.event.HasSaveHandlers;
import fr.km.client.event.SaveEvent;
import fr.km.client.event.SaveHandler;
import fr.km.client.model.Article;

public class ArticleEditorDisplay extends Composite implements HasSaveHandlers, HasCloseHandlers<ArticleEditorDisplay>, ClickHandler{
	private Button saveButton;
    private Button cancelButton;
    private TextBox title;
    private TextBox tags;
    private TextArea knowledge;
    
	public ArticleEditorDisplay(final Article article) {
		// Create a table to layout the form options
	    FlexTable layout = new FlexTable();
	    layout.setCellSpacing(6);

	    title = new TextBox();
	    title.setText(article.getTitle());
	    tags = new TextBox();
	    
	    String tagString = new String();
	    ArrayList<String> tagList = article.getTags();
	    Iterator<String> iterator = tagList.iterator();
	    while(iterator.hasNext()) {
	    	tagString += iterator.next();
	    	if(iterator.hasNext()) {
	    		tagString += ", ";
	    	}
	    }
	    
	    tags.setText(tagString);
	    
	    knowledge = new TextArea();
	    knowledge.setText(article.getContent());
	    
	    layout.setHTML(0, 0, "Title:");
	    layout.setWidget(0, 1, title);
	    layout.setHTML(1, 0, "Tags:");
	    layout.setWidget(1, 1, tags);
	    layout.setHTML(2, 0, "Knowledge");
	    layout.setWidget(2, 1, knowledge);
	    saveButton = new Button("Save");
	    cancelButton = new Button("Cancel");
	    saveButton.addClickHandler(this);
	    cancelButton.addClickHandler(this);
	    
	    layout.setWidget(3, 0, saveButton);
	    layout.setWidget(3, 1, cancelButton);
		initWidget(layout);
		
	}

	@Override
	public HandlerRegistration addSaveHandler(SaveHandler handler) {
		return addHandler(handler, SaveEvent.getType());
	}

	@Override
	public HandlerRegistration addCloseHandler(
			CloseHandler<ArticleEditorDisplay> handler) {
		return addHandler(handler, CloseEvent.getType());
	}

	@Override
	public void onClick(ClickEvent event) {
		Button source = (Button) event.getSource();
		if(source == saveButton) {
			fireEvent(new SaveEvent());
		} else {
			CloseEvent.fire(this, this);
		}
	}
	
	public String getTitle() {
		return title.getText();
	}
	
	public ArrayList<String> getTags() {
		String text = tags.getText();
		String[] split = text.split(",");
		ArrayList<String> tagList = new ArrayList<String>();
		for(String tag : split) {
			String trim = tag.trim();
			if(!trim.equals("")) {
				tagList.add(trim);
			}
		}
		
		return tagList;
	}
	
	public String getKnowledge() {
		return knowledge.getText();
	}
}
